package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFromToEvent;

import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class BanFlowFlag extends JavaLandFlag implements Listener{
	
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.NONE;
	}
	@Override
	public ELandFlagType getELandFlagType() {
		return ELandFlagType.PLAYER;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
	}
	@EventHandler
	public void onConfigLoad(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "禁止岩浆/水流动");
			config.set("lore", Arrays.asList("&7（禁止领地内的岩浆/水流动）"));
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
	}
	
	@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
	public void onBlockFromTo(final BlockFromToEvent e) {
		final Land land_from = ILandManager.getILandManager().getHighestPriorityLand(e.getBlock().getLocation());
		if(land_from != null && JavaLandFlag.hasLandFlag(land_from, this)) {
			e.setCancelled(true);
			return;
		}
		final Land land_to = ILandManager.getILandManager().getHighestPriorityLand(e.getToBlock().getLocation());
		if(land_to != null && !land_to.equals(land_from) && JavaLandFlag.hasLandFlag(land_to, this)) {
			e.setCancelled(true);
			return;
		}
	}
}